﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcControlToolkit
{
    /// <summary>
    /// DropDown is an ASP.NET AJAX extender that can be attached to almost any 
    /// ASP.NET control to provide a SharePoint-style drop-down menu. The displayed 
    /// menu is merely another panel or control.
    /// </summary>
    public static class DropDownExtensions
    {
        /// <summary>
        /// Creates a DropDown extender
        /// </summary>
        /// <param name="elementId">The ID of the control which will be displayed as the dropdown</param>
        /// <param name="dropDownControlId">The ID of the control which will be displayed for the dropdown</param>
        public static string DropDown(this AjaxHelper helper, string elementId, string dropDownControlId)
        {
            return helper.DropDown(
                    elementId,
                    new { dropDownControl = new ElementReference(dropDownControlId) }
                );
        }

        /// <summary>
        /// Creates a DropDown extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control which will be displayed as the dropdown</param>
        /// <param name="properties">An object containing the properties to be set</param>        
        public static string DropDown(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add Css Reference
            sb.Append(helper.DynamicToolkitCssInclude("AjaxControlToolkit.DropDown.DropDown.css"));

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js",
                    "AjaxControlToolkit.Compat.Timer.Timer.js",
                    "AjaxControlToolkit.Animation.Animations.js",
                    "AjaxControlToolkit.Animation.AnimationBehavior.js",
                    "AjaxControlToolkit.PopupExtender.PopupBehavior.js",
                    "AjaxControlToolkit.HoverExtender.HoverBehavior.js",
                    "AjaxControlToolkit.DropDown.DropDownBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.DropDownBehavior", properties, elementId));

            return sb.ToString();
        }
    }
}
